package alp.starcode.edms.framework.handler;

import alp.starcode.edms.framework.base.model.LoginUser;
import alp.starcode.edms.framework.model.UserSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@Component
public class CurrentUserComponentHandler extends CurrentUserComponent<LoginUser> {


    /**
     * 获取当前登录用户Id
     *
     * @return 用户Id
     */
    @Override
    public String getCurrentUserId() {
        return getCurrentUser().getId();
    }

    /**
     * 获取当前登录用户姓名
     *
     * @return 用户姓名
     */
    @Override
    public String getCurrentUserName() {
        return getCurrentUser().getUsername();
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 用户信息
     */
    @Override
    public UserSession getCurrentUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        HttpSession session = request.getSession(false);
        UserSession userSession = (UserSession) session.getAttribute("userSession");
        return userSession;
    }
}
